//
//  NSString+TL.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/22.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "NSString+TL.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (TL)

- (NSString *)trimWhitespace
{
    NSMutableString *str = [self mutableCopy];
    CFStringTrimWhitespace((__bridge CFMutableStringRef)str);
    return str;
}

- (NSString *)sha1Str
{
    const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:self.length];
    
    uint8_t digest[CC_SHA1_DIGEST_LENGTH];
    
    CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
    
    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return output;
}

- (NSDate *)dateWithStr
{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
    [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate *date = [inputFormatter dateFromString:self];
    return date;
}

- (NSDate *)dateWithTimeSp:(NSString *)timeSp
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    NSDate *confromDate = [NSDate dateWithTimeIntervalSince1970:[timeSp floatValue]/1000];
    return confromDate;
}

- (NSRange)rangeByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet
{
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];
    [self getCharacters:charBuffer];
    for (length = [self length]; length > 0; length--) {
        if (![characterSet characterIsMember:charBuffer[length - 1]]) {
            break;
        }
    }
    return NSMakeRange(location, length - location);
}

- (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    CGSize resultSize = CGSizeZero;
    if (self.length <= 0) {
        
        return resultSize;
    }
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    resultSize = [self boundingRectWithSize:CGSizeMake(size.width, size.height)
                                             options:(NSStringDrawingUsesLineFragmentOrigin)
                                          attributes:@{NSFontAttributeName: font, NSParagraphStyleAttributeName: style}
                                             context:nil].size;
    return resultSize;
}

- (CGFloat)getWidthWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    return [self getSizeWithFont:font constrainedToSize:size].width;
}

- (CGFloat)getHeightWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    return [self getHeightWithFont:font constrainedToSize:size];
}

/**
 *  生成不同颜色的文字
 */
- (NSMutableAttributedString *)arrributedStringWithMultipleChineseColor:(UIColor *)color1 andOtherColor:(UIColor *)color2
{
    NSArray *number = @[@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @".", @"+", @"*"];
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:self];
    for (int i = 0; i < self.length; i ++) {
        //这里的小技巧，每次只截取一个字符的范围
        NSString *str = [self substringWithRange:NSMakeRange(i, 1)];
        //判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符，从而筛选出符合要求的数字字符的范围NSMakeRange
        if ([number containsObject:str]) {
            [attributeString setAttributes:@{NSForegroundColorAttributeName:color2, NSFontAttributeName:[UIFont systemFontOfSize:14],NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]} range:NSMakeRange(i, 1)];
        }else{
            [attributeString setAttributes:@{NSForegroundColorAttributeName:color1, NSFontAttributeName:[UIFont systemFontOfSize:12],NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]} range:NSMakeRange(i, 1)];
        }
    }
    return attributeString;
}

@end
